package com.rs.adminweb.log;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;

import com.jaxplat.commons.Row;
import com.jaxplat.exceptions.GeneralException;
import com.rs.commons.web.AdminBaseAction;
import com.rs.log.delegate.RsLogDelegate;

public class LogAction extends AdminBaseAction
{
    private Log log = LogFactory.getLog(getClass());
    private RsLogDelegate logDelegate;

    public RsLogDelegate getLogDelegate()
    {
        return logDelegate;
    }

    public void setLogDelegate(RsLogDelegate logDelegate)
    {
        this.logDelegate = logDelegate;
    }

    public ModelAndView queryAdminLog(HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        JSONObject json = new JSONObject();
        try
        {
            Object[] result = this.getLogDelegate().queryAdminLog(param);
            json.element("total", ((Integer) result[1]).intValue());
            json.element("rows", ((List) result[0]));
        }
        catch (GeneralException ex)
        {
            log.error(ex);
        }
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print(json.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }

    public ModelAndView queryUserLog(HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        JSONObject json = new JSONObject();
        try
        {
            Object[] result = this.getLogDelegate().queryUserLog(param);
            json.element("total", ((Integer) result[1]).intValue());
            json.element("rows", ((List) result[0]));
        }
        catch (GeneralException ex)
        {
            log.error(ex);
        }
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print(json.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }

    public ModelAndView deleteAdminLog(HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        JSONObject json = new JSONObject();
        try
        {
            this.getLogDelegate().delAdminLog(param);
            json.element("errcode", "0");
            json.element("message", "成功");
        }
        catch (GeneralException ex)
        {
            json.element("errcode", "1");
            json.element("message", "删除不成功");
            log.error(ex);
        }
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print(json.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }

    public ModelAndView deleteUserLog(HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        JSONObject json = new JSONObject();
        try
        {
            this.getLogDelegate().delAdminLog(param);
            json.element("errcode", "0");
            json.element("message", "成功");
        }
        catch (GeneralException ex)
        {
            json.element("errcode", "1");
            json.element("message", "删除不成功");
            log.error(ex);
        }
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print(json.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }
}
